package com.yss;

import com.yss.netty.NettyRemotingServer;
import com.yss.netty.config.NettyServerConfig;
import com.yss.netty.protocol.CommandType;
import com.yss.processor.BondProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class ServerApplication {

    @Autowired
    private BondProcessor bondProcessor;

    @Value("${netty.port}")
    private int nettyPort;

    @PostConstruct
    public void init() {
        startNettyServer();
        registerNettyHandlerProcessor();
    }

    private NettyRemotingServer nettyRemotingServer;

    public static void main(String[] args) {
        SpringApplication sa = new SpringApplication(ServerApplication.class);
        sa.run(args);
    }

    private void startNettyServer() {
        NettyServerConfig serverConfig = new NettyServerConfig();
        serverConfig.setListenerPort(nettyPort);
        this.nettyRemotingServer = new NettyRemotingServer(serverConfig);
        nettyRemotingServer.start();
    }

    private void registerNettyHandlerProcessor() {
        // Use default thread pool
        nettyRemotingServer.registerProcessors(CommandType.BOND, this.bondProcessor);
    }

}
